package com.neusoft.neuiotms.dcom.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.DutyArrangementGroupDTO;
import com.neusoft.neuiotms.dcom.model.DutyArrangementGroup;
import com.neusoft.neuiotms.dcom.model.TeamGroup;
import com.neusoft.neuiotms.dcom.repository.TeamGroupRepository;

@Component
public class DutyArrangementGroupConverter extends AbstractConverter<DutyArrangementGroup, DutyArrangementGroupDTO> {

    @Autowired
    private TeamGroupRepository teamGroupRepository;

    @Override
    public DutyArrangementGroup toModel(final DutyArrangementGroupDTO dto) {

        final DutyArrangementGroup model = new DutyArrangementGroup();
        model.setGroupNumber(dto.getGroupNumber());

        if (null != dto.getTeamGroupId()) {
            final TeamGroup teamGroupModel = this.teamGroupRepository.getOne(dto.getTeamGroupId());
            model.setTeamGroup(teamGroupModel);
        }

        return model;
    }

    @Override
    public DutyArrangementGroupDTO toDTO(final DutyArrangementGroup model, final boolean forListView) {

        final DutyArrangementGroupDTO dto = new DutyArrangementGroupDTO();

        dto.setId(model.getId());
        dto.setGroupNumber(model.getGroupNumber());

        if (model.getGroup() != null) {
            dto.setTeamGroupId(model.getTeamGroup().getId());
            dto.setTeamGroupName(model.getTeamGroup().getName());
        }

        return dto;
    }

}
